package com.samba.entites;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries; 
import javax.persistence.NamedQuery;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


@Entity
@NamedQueries( {
		@NamedQuery(name = "findAllProjets", query = "SELECT p FROM Projet  p ORDER BY p.dateDePublication"),
		@NamedQuery(name = "findProjetById", query = "SELECT p FROM Projet p WHERE p.id = :id"),
		@NamedQuery(name = "findAllAvailableProjets", query = "SELECT p FROM Projet p  WHERE p.id =:id AND p.valid = true AND p.active = true AND 0 = "
				+ "(SELECT COUNT(lent) "
				+ "FROM CreditProjetUtilisateur lent "
				+ "WHERE lent.projet = p AND NOT (lent.etat = :etat)) ORDER BY p.dateDePublication")
		})
@Table(name= "TABLE_PROJET")
@SequenceGenerator(name = "SeqProjet" , sequenceName= "projet_seq")
public class Projet implements Serializable {
	@Id 
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SeqProjet")
	private int id;
	
	
	private String auteur;
	private String tiltle;
	private Date dateDePublication ; 
	private int defaultDurePret;
	private boolean valid;
	private boolean active;
	private String details;
	private String entreprise;
	private byte[] logo;
	private long valeurProjet;
    
 
    @ManyToOne	
	@JoinColumn(name ="CATEGORY_FK")
	private Category categoryFk;
	
	@ManyToOne
	@JoinColumn(name ="TYPE_FK")
	private Type typeFk;
	
	
	@ManyToMany
	@JoinTable(
			name= "TABLE_PROJET_PRETEUR",
			joinColumns=
			@JoinColumn(name = "PROJET_ID", referencedColumnName = "ID"),
		inverseJoinColumns=
		@JoinColumn(name= "PRETEUR_ID",referencedColumnName= "ID"))
	private Set<Utilisateur> preteurs;
	
	public Projet()
	{ 
		typeFk = new Type();
		setCategoryFk(new Category());
		preteurs = new HashSet<Utilisateur>();
	}
	
	private static final long serialVersionUID = 1L;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAuteur() {
		return auteur;
	}

	public void setAuteur(String auteur) {
		this.auteur = auteur;
	}

	public String getTiltle() {
		return tiltle;
	}
	
	public String getEntreprise() {
		return entreprise;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}
	public void setEntreprise(String entreprise) {
		this.entreprise = entreprise;
	}

	public void setTiltle(String tiltle) {
		this.tiltle = tiltle;
	}

	@Temporal(TemporalType.DATE)
	public Date getDateDePublication() {
		return dateDePublication;
	}

	public void setDateDePublication(Date dateDePublication) {
		this.dateDePublication = dateDePublication;
	}

	public int getDefaultDurePret() {
		return defaultDurePret;
	}

	public void setDefaultDurePret(int defaultDurePret) {
		this.defaultDurePret = defaultDurePret;
	}

	public boolean isValid() {
		return valid;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	public boolean getActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}


	
    @Lob
    @Basic(fetch = FetchType.LAZY)
	public byte[] getLogo() {
		return logo;
	}

	public void setLogo(byte[] logo) {
		this.logo = logo;
	}
	public long getValeurProjet() {
		return valeurProjet;
	}

	public void setValeurProjet(long valeurProjet) {
		this.valeurProjet = valeurProjet;
	}

 

	

public Type getTypeFk() {
		return typeFk;
	}

	public void setTypeFk(Type typeFk) {
		this.typeFk = typeFk;
	}
	
public Set<Utilisateur> getPreteurs() {
		return preteurs;
	}


	public void setPreteurs(Set<Utilisateur> preteurs) {
		this.preteurs = preteurs;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Projet other = (Projet) obj;
		if (id != other.id)
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Projet [tiltle=" + tiltle + "]";
	}

	public Category getCategoryFk() {
		return categoryFk;
	}

	public void setCategoryFk(Category categoryFk) {
		this.categoryFk = categoryFk;
	}


	
}
